package com.project.card;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.project.card.bean.UserBean;

import java.util.Random;

import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class HelpActivity extends AppCompatActivity {
    private Button btn_confirm;

    private EditText editName;
    private EditText editEthnic;
    private EditText editBirth;
    private EditText editAddr;
    private EditText editCode;
    private EditText editComment;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_help);
        
        btn_confirm = findViewById(R.id.help_confirm);

        editName = findViewById(R.id.help_edit_name);
        editEthnic = findViewById(R.id.help_edit_ethnic);
        editBirth = findViewById(R.id.help_edit_birth);
        editAddr = findViewById(R.id.help_edit_addr);
        editCode = findViewById(R.id.help_edit_code);
        editComment = findViewById(R.id.help_edit_comment);

        OkHttpClient client = new OkHttpClient();
        
        
        btn_confirm.setOnClickListener(v -> {
            UserBean user = new UserBean();
            user.setPassword("123456");
            user.setCommentCode(genCommentCode());
            user.setBalance(0);
            user.setSchedule(0);
            user.setCode(editCode.getText().toString());
            Toast.makeText(getApplicationContext(),user.getCommentCode(),Toast.LENGTH_LONG).show();
            
//            new Thread(new Runnable() {
//                @Override
//                public void run() {
//                    try{
//                        FormBody.Builder params = new FormBody.Builder();
//                        params.add("password","123456");
//                        params.add("commentCode",genCommentCode());
//                        params.add("code",editCode.getText().toString());
//                        params.add("bonus",editComment.getText().toString());
//                        Request request = new Request.Builder()
//                                .url("http://192.168.1.9:8080/user/regh")
//                                .post(params.build())
//                                .build();
//                        Response response = client.newCall(request).execute();
//                    }catch (Exception e){
//                        e.printStackTrace();
//                    }
//                }
//            }).start();
        });
        
    }

    public static String genCommentCode(){
        int  maxNum = 36;
        int i;
        int count = 0;
        char[] str = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K',
                'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W',
                'X', 'Y', 'Z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' };
        StringBuffer gen = new StringBuffer("");
        Random r = new Random();
        while(count < 8){
            i = Math.abs(r.nextInt(maxNum));
            if (i >= 0 && i < str.length) {
                gen.append(str[i]);
                count ++;
            }
        }
        return gen.toString();
    }
}